package co.lostreseditores.colegio.dataaccess.dao;

import co.lostreseditores.colegio.dataaccess.sessionFactory.HibernateSessionFactory;
import co.lostreseditores.colegio.modelo.Colegio;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;

import org.hibernate.criterion.Example;

import java.math.BigDecimal;

import java.util.Date;
import java.util.List;
import java.util.Set;


/**
 * A data access object (DAO) providing persistence and search support for
 * Colegio entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 *
 *
 * @author Zathura Code Generator http://code.google.com/p/zathura
 *
 */
public class ColegioDAO implements IColegioDAO {
    private static final Log log = LogFactory.getLog(ColegioDAO.class);

    // property constants
    //public static final Long  COLECODDANEN = "colecoddanen";
    public static final String COLECODDANEN = "colecoddanen";

    //public static final String  COLEDESCRIPV = "coledescripv";
    public static final String COLEDESCRIPV = "coledescripv";

    //public static final String  COLEDIRECCIONV = "coledireccionv";
    public static final String COLEDIRECCIONV = "coledireccionv";

    //public static final String  COLEEMAILV = "coleemailv";
    public static final String COLEEMAILV = "coleemailv";

    //public static final String  COLEFAXV = "colefaxv";
    public static final String COLEFAXV = "colefaxv";

    //public static final Integer  COLEIDN = "coleidn";
    public static final String COLEIDN = "coleidn";

    //public static final String  COLELOGOV = "colelogov";
    public static final String COLELOGOV = "colelogov";

    //public static final String  COLESECTORV = "colesectorv";
    public static final String COLESECTORV = "colesectorv";

    //public static final String  COLETELEFONOFV = "coletelefonofv";
    public static final String COLETELEFONOFV = "coletelefonofv";

    //public static final String  COLEZONAV = "colezonav";
    public static final String COLEZONAV = "colezonav";

    //public static final Integer  COMUIDN = "comuidn";
    public static final String COMUIDN = "comuidn";

    //public static final Long  LUGAIDN = "lugaidn";
    public static final String LUGAIDN = "lugaidn";

    private Session getSession() {
        return HibernateSessionFactory.getSession();
    }

    /**
    *
    * @param Instance
    *            Colegio Instance to persist
    * @throws RuntimeException
    *             when the operation fails
    */
    public void save(Colegio instance) {
        log.debug("saving Colegio instance");

        try {
            getSession().save(instance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    /**
    * @param Instance
    *            Colegio Instance to delete
    * @throws RuntimeException
    *             when the operation fails
    */
    public void delete(Colegio instance) {
        log.debug("deleting Colegio instance");

        try {
            getSession().delete(instance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    /**
    *
    * @param Instance
    *            Colegio Instance to update
    * @throws RuntimeException
    *             when the operation fails
    */
    public void update(Colegio instance) {
        log.debug("updating Colegio instance");

        try {
            getSession().update(instance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public Colegio findById(Integer id) {
        log.debug("finding Colegio instance with id: " + id);

        try {
            Colegio instance = (Colegio) getSession().get(Colegio.class, id);

            return instance;
        } catch (RuntimeException re) {
            log.error("finding Colegio failed", re);
            throw re;
        }
    }

    public List<Colegio> findByExample(Colegio instance) {
        log.debug("finding Colegio instance by example");

        try {
            List results = getSession().createCriteria("Colegio")
                               .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " +
                results.size());

            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    /**
    * Find all  Colegio entities with a specific property value.
    *
    * @param value
    *            the property value to match
    * @param propertyName
    *            the property to search in the instance
    * @return List< Colegio> found by query
        */
    public List<Colegio> findByProperty(String propertyName, Object value) {
        log.debug("finding Colegio instance with property: " + propertyName +
            ", value: " + value);

        try {
            String queryString = "from Colegio as model where model." +
                propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<Colegio> findByColecoddanen(Object colecoddanen) {
        return findByProperty(COLECODDANEN, colecoddanen);
    }

    public List<Colegio> findByColedescripv(Object coledescripv) {
        return findByProperty(COLEDESCRIPV, coledescripv);
    }

    public List<Colegio> findByColedireccionv(Object coledireccionv) {
        return findByProperty(COLEDIRECCIONV, coledireccionv);
    }

    public List<Colegio> findByColeemailv(Object coleemailv) {
        return findByProperty(COLEEMAILV, coleemailv);
    }

    public List<Colegio> findByColefaxv(Object colefaxv) {
        return findByProperty(COLEFAXV, colefaxv);
    }

    public List<Colegio> findByColeidn(Object coleidn) {
        return findByProperty(COLEIDN, coleidn);
    }

    public List<Colegio> findByColelogov(Object colelogov) {
        return findByProperty(COLELOGOV, colelogov);
    }

    public List<Colegio> findByColesectorv(Object colesectorv) {
        return findByProperty(COLESECTORV, colesectorv);
    }

    public List<Colegio> findByColetelefonofv(Object coletelefonofv) {
        return findByProperty(COLETELEFONOFV, coletelefonofv);
    }

    public List<Colegio> findByColezonav(Object colezonav) {
        return findByProperty(COLEZONAV, colezonav);
    }

    public List<Colegio> findByComuidn(Object comuidn) {
        return findByProperty(COMUIDN, comuidn);
    }

    public List<Colegio> findByLugaidn(Object lugaidn) {
        return findByProperty(LUGAIDN, lugaidn);
    }

    /**
    * Find all Colegio entities.
    *
    * @return List<Colegio> all Colegio instances
    */
    public List<Colegio> findAll() {
        log.debug("finding all Colegio instances");

        try {
            String queryString = "from Colegio";
            Query queryObject = getSession().createQuery(queryString);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public List<Colegio> findByCriteria(String whereCondition) {
        log.debug("finding Colegio " + whereCondition);

        try {
            String where = ((whereCondition == null) ||
                (whereCondition.length() == 0)) ? "" : ("where " +
                whereCondition);
            final String queryString = "select model from Colegio model " +
                where;
            Query query = getSession().createQuery(queryString);
            List<Colegio> entitiesList = query.list();

            return entitiesList;
        } catch (RuntimeException re) {
            log.error("find By Criteria in Colegio failed", re);
            throw re;
        }
    }

    public List<Colegio> findPageColegio(String sortColumnName,
        boolean sortAscending, int startRow, int maxResults) {
        log.debug("finding Colegio findPageColegio");

        if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
            try {
                String queryString = "select model from Colegio model order by model." +
                    sortColumnName + " " + (sortAscending ? "asc" : "desc");

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        } else {
            try {
                String queryString = "select model from Colegio model";

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        }
    }

    public Long findTotalNumberColegio() {
        log.debug("finding Colegio count");

        try {
            String queryString = "select count(*) from Colegio model";
            Query queryObject = getSession().createQuery(queryString);

            return (Long) queryObject.list().get(0);
        } catch (RuntimeException re) {
            throw re;
        }
    }
}
